(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('yly-form-source/lib/form-source')) :
  typeof define === 'function' && define.amd ? define(['yly-form-source/lib/form-source'], factory) :
  (global = global || self, global['yly-form-mixin'] = factory(global.formSource));
}(this, (function (formSource) { 'use strict';

  var FormMixin = {
    props: {
      // 这个 value 要命名规范化
      value: {
        type: Object,
        required: true
      },
      formId: {
        type: String,
        default: ''
      },
      formActionType: {
        type: String,
        default: 'baseForm' // baseForm 基础表单 // displayForm 展示表单组件 // TableForm
      },
      formCode: {
        type: String,
        required: true
      }
    },
    data() {
      return {
        form: null,
        formParams: {},
        sourceId: '',
        formSource: null,
        formBaseInfo: {
          formId: this.formId,
          projectId: this.$route.query.projectId,
          formCode: this.formCode
        },
        cloneFormData: {},
        groupConfigObj: {
          basicsArray: [],
          layoutArray: [],
          bussinessArray: []
        },
        renderClient: 'WEB',
        renderScene: 'edit'
      };
    },
    computed: {},
    created() {
      if (this.renderClient !== 'mobile') {
        this.form = this.$form.createForm(this, {
          onValuesChange: (_, values) => {
            if (values) {
              for ( var key in values) {
                if (values.hasOwnProperty(key)) {
                  if (!this.cloneFormData.hasOwnProperty(key) || this.cloneFormData[key] !== values[key]) {
                    this.$set(this.cloneFormData, key, values[key]);
                  }
                }
              }
            }
          }
        });
      }
      this.initFormParams();
      this.sourceId = `sourceId:formCode-${this.formCode}-formId-${this.formId}`;
      if (this.renderScene === 'ide') {
        this.initGroupList();
      }
      this.initFormSource();
      this.initForm();
    },
    methods: {
      initFormParams() {},
      initGroupList() {
        var groupConfigList = formSource.FormSource.FormComponentsControl.getGroupConfig('DEFAULT_FORM_GROUP');
        if (Array.isArray(groupConfigList)) {
          /**
           * this.groupConfigObj.basicsArray = groupConfigList.find(item => item.groupType === 'basicsList')?.groupCompList || []
          this.groupConfigObj.layoutArray = groupConfigList.find(item => item.groupType === 'layoutList')?.groupCompList || []
          this.groupConfigObj.bussinessArray = groupConfigList.find(item => item.groupType === 'businessList')?.groupCompList || []
          */
          var basicsList = groupConfigList.find(item => item.groupType === 'basicsList');
          this.groupConfigObj.basicsArray = basicsList ? basicsList.groupCompList : [];
          var layoutList = groupConfigList.find(item => item.groupType === 'layoutList');
          this.groupConfigObj.layoutArray = layoutList ? layoutList.groupCompList : [];
          var businessList = groupConfigList.find(item => item.groupType === 'businessList');
          this.groupConfigObj.bussinessArray = businessList ? businessList.groupCompList : [];
          if (this.renderClient === 'MOBILE') {
            this.groupConfigObj.layoutArray = this.groupConfigObj.layoutArray.filter(item => !['FORM_GRID', 'FORM_MY_FIELD'].includes(item.code));
            this.groupConfigObj.bussinessArray = this.groupConfigObj.bussinessArray.filter(item => !['FORM_USER_SELECT', 'FORM_ORG_SELECT'].includes(item.code));
          }
        }
      },
      initFormSource() {
        this.formSource = formSource.FormSource.getInstance(this.sourceId, this.formId, this.formCode, this);
        this.initSourceContent();
        // 初始化表单实例
        this.formSource.initFormRef(this.form);
      },
      initForm() {
        this.formSource.formDataControl.initFormConfig(this.value);
        this.formSource.formDataControl.initFormData(this.value);
      },
      initSourceContent() {
        var sourceContent = {
          config: this.value && this.value.config,
          instance: {
            // 存放表单的重要标识
            sourceId: this.sourceId,
            formId: this.formBaseInfo && this.formBaseInfo.formId,
            projectId: this.formBaseInfo && this.formBaseInfo.projectId,
            dbId: this.formBaseInfo && this.formBaseInfo.dbId,
            formCode: this.formCode
          },
          renderScene: this.renderScene,
          renderClient: this.renderClient,
          // 渲染客户端
          selectedComponent: {},
          // 当前选中的组件
          formActionType: this.formActionType // 表单执行类型
        };
        this.formSource.initSourceContent(sourceContent);
      }
    },
    beforeDestroy() {
      console.log('destroy FormSource');
      formSource.FormSource.deleteInstance(this.sourceId);
    }
  };

  return FormMixin;

})));
